package com.yjhh.scala

/**
  *
  *
  */
object HelloCollectionOpts extends App {

  //从左往右操作的结果值作为第二次运算的左边的输入值
  println((1 to 5).reduceLeft(_ - _))
  (1 to 5).reduceLeft((a, b) => { println(a + "  " + b);a - b})

  //从右往左操作的结果值作为第二次运算的右边的输入值
  println((1 to 5).reduceRight(_ - _))
  (1 to 5).reduceRight((a, b) => { println(a + "  " + b);a - b})


  println((1 to 5).foldLeft(10)(_ - _))
  (1 to 5).foldLeft(10)((a, b) => { println(a + "  " + b);a - b})

  println((1 to 5).foldRight(10)(_ - _))
  (1 to 5).foldRight(10)((a, b) => { println(a + "  " + b);a - b})

  (1 to 5).foreach(println)

  var pf = (1 to 5).collect{case 1 => "Scala";case 2 => "Spark"}
  println(pf)

  (1 to 5).map(println)

  val scores = Map("Spark"->List(100,99,98),"Kafka"->List(99,98),"Hadoop"->List(70,68))
  var keys = List("Spark","Kafka","Hadoop")

  println(keys.map(scores(_)))   //获取key对应的values

  var dd = keys.flatMap(key => scores(key))
  println(dd)

  def odd(num : Int) = {
    if(num%2==0){
      true
    }else{
      false
    }
  }

  println((1 to 5).filter(odd))

  println((1 to 5).filter(x => x%2==0))

  println((1 to 5).filterNot(x => x%2==0))


}
